package com.kwin;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;

public class ClusterPoolUtil {
    private static JedisCluster jedisCluster;

    public static JedisCluster getJedisCluster() {
        if (jedisCluster == null) {
            int timout = 10000;
            HashSet<HostAndPort> nodes = new HashSet<HostAndPort>();
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxTotal(200);
            poolConfig.setMaxIdle(200);
            poolConfig.setMaxWaitMillis(1000 * 100);
            poolConfig.setTestOnBorrow(false);

            nodes.add(new HostAndPort("106.53.96.171", 6379));
            nodes.add(new HostAndPort("106.53.96.171", 6380));
            nodes.add(new HostAndPort("106.53.96.171", 6381));
            nodes.add(new HostAndPort("106.53.96.171", 6389));
            nodes.add(new HostAndPort("106.53.96.171", 6390));
            nodes.add(new HostAndPort("106.53.96.171", 6391));

            jedisCluster = new JedisCluster(nodes, timout, poolConfig);
        }

        return jedisCluster;
    }

    public static void main(String[] args) {
        JedisCluster jedisCluster = getJedisCluster();
        System.out.println(jedisCluster.set("date", "2021-6-17"));
        System.out.println(jedisCluster.get("date"));
        jedisCluster.close();
    }
}
